package com.hm.myrecyclervview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView


class MyAdapter(private val context: Context, private val starList: List<MyData>?) :
    RecyclerView.Adapter<MyAdapter.StarViewHolder>() {

    /**
     * 是否是组的第一个item
     *
     * @param position
     * @return
     */
    fun isGourpHeader(position: Int): Boolean {
        if (position == 0) {
            return true
        } else {
            val currentGroupName = getGroupName(position)
            val preGroupName = getGroupName(position - 1)
            return if (preGroupName == currentGroupName) {
                false
            } else {
                true
            }
        }
    }

    fun getGroupName(position: Int): String {
        return starList!![position].group
    }


    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): StarViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.rv_item_star, null)
        return StarViewHolder(view)
    }

    override fun onBindViewHolder(
        holder: StarViewHolder,
        position: Int,
    ) {

      holder.tv.setText(starList!![position].id)
    }

    override fun getItemCount(): Int {
        return starList?.size ?: 0
    }

    class StarViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         val tv: TextView = itemView.findViewById(R.id.tv_star)
    }
}